package com.qztc.hire_end_core.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.qztc.hire_end_core.mapper.MemberPrivilegeMapper;
import com.qztc.hire_end_dto.dto.MemberPrivilege;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;

@RestController
@CrossOrigin
@RequestMapping("/memberPrivilege")
@Tag(name = "会员权限管理接口")
public class MemberPrivilegeController {
    @Autowired
    private MemberPrivilegeMapper memberPrivilegeMapper;

    @Operation(summary = "查询所有会员权限")
    @GetMapping("/getAll")
    public List<MemberPrivilege> getAll() {
        return new MemberPrivilege().selectAll();
    }

    @Operation(summary = "根据memberPrivilegeId获取具体会员权限")
    @GetMapping("/getbymemberPrivilegeId")
    public MemberPrivilege getbymemberPrivilegeId(@RequestParam Integer memberPrivilegeId) {
        QueryWrapper<MemberPrivilege> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("member_privilege_id", memberPrivilegeId);
        return new MemberPrivilege().selectOne(queryWrapper);
    }

    @Operation(summary = "根据memberPrivilegeId删除某一个具体会员权限")
    @DeleteMapping("/delete")
    public boolean delete(@RequestBody MemberPrivilege memberPrivilege) {
        return memberPrivilege.deleteById(memberPrivilege.getMemberPrivilegeId());
    }

    @Operation(summary = "根据memberPrivilegeId修改某一个具体会员权限")
    @PutMapping("/update")
    public boolean update(@RequestBody MemberPrivilege memberPrivilege) {
        QueryWrapper<MemberPrivilege> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("member_privilege_describe", memberPrivilege.getMemberPrivilegeDescribe());
        if(memberPrivilege.selectCount(queryWrapper)>0){
            return false;
        }
        return memberPrivilege.updateById();
    }

    @Operation(summary = "增加会员权限")
    @PostMapping("/add")
    public boolean add(@RequestBody MemberPrivilege memberPrivilege) {
        QueryWrapper<MemberPrivilege> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("member_privilege_describe", memberPrivilege.getMemberPrivilegeDescribe());
        if(memberPrivilege.selectCount(queryWrapper)>0){
            return false;
        }
        return memberPrivilege.insert();
    }

    @Operation(summary = "批量删除会员权限")
    @DeleteMapping("/deleteMany")
    public boolean deleteMany(@RequestBody List<Integer> memberPrivilegeIds) {
        if (memberPrivilegeIds == null || memberPrivilegeIds.isEmpty()) {
            return false;
        }
        QueryWrapper<MemberPrivilege> wrapper = new QueryWrapper<>();
        wrapper.in("member_privilege_id", memberPrivilegeIds);
        return new MemberPrivilege().delete(wrapper);
    }

    @Operation(summary = "批量增加会员权限")
    @PostMapping("/addMany")
    public boolean addMany(@RequestBody List<MemberPrivilege> memberPrivileges) {
        if (memberPrivileges == null || memberPrivileges.isEmpty()) {
            return false;
        }
        // 批量重复校验
        QueryWrapper<MemberPrivilege> queryWrapper = new QueryWrapper<>();
        queryWrapper.in("member_privilege_describe",
                memberPrivileges.stream().map(MemberPrivilege::getMemberPrivilegeDescribe).collect(Collectors.toList()));
        if (memberPrivilegeMapper.selectCount(queryWrapper) > 0) {
            return false;
        }
        // 批量插入SQL
        String sql = "insert into member_privilege (member_privilege_id, member_privilege_name) values" +
                memberPrivileges.stream()
                        .map(privilege -> String.format("(%d,'%s')", privilege.getMemberPrivilegeId(), privilege.getMemberPrivilegeDescribe()))
                        .collect(Collectors.joining(","));
        return memberPrivilegeMapper.executeSql(sql);
    }
}